﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PMS.XCMG.Entity;
using PMS.XCMG.IBusiness;
using PMS.XCMG.Util;
using PMS.XCMG.Models;

namespace PMS.XCMG.Business
{
    public class PlanBusiness : BusRepository<Plan>, IPlanBusiness, IScopedDependency
    {
        private IServiceProvider SvcProvider { get; set; }
        public PlanBusiness(GDbContext context, IServiceProvider svcProvider)
            : base(context)
        {
            this.SvcProvider = svcProvider;
        }

        public async Task<PageResult<Plan>> GetPageResultAsync(PageInput<PlanQM> query)
        {
            try
            {
                var queryable = this.GetQueryable(true);
                queryable = queryable
                    .Include(i => i.Bom)
                        .ThenInclude(ti => ti.Product)
                    .Include(i => i.Bom)
                        .ThenInclude(ti => ti.Spec);

                var search = query.Search;
                if (!search.Keyword.IsNullOrEmpty())
                    queryable = queryable.Where(w => w.Name.Contains(search.Keyword) || w.Code.Contains(search.Keyword));

                if (search.Status > -1)
                    queryable = queryable.Where(w => w.Status == GetStatus(search.Status));

                int iStart = 0, iEnd = 0;
                if (string.IsNullOrWhiteSpace(search.StartDate)) search.StartDate = DateTime.Now.ToString("yyyy-MM-01");
                if (string.IsNullOrWhiteSpace(search.EndDate)) search.EndDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");

                iStart = Convert.ToInt32(Convert.ToDateTime(search.StartDate).ToString("yyyyMMdd"));
                iEnd = Convert.ToInt32(Convert.ToDateTime(search.EndDate).ToString("yyyyMMdd"));

                queryable = queryable.Where(p => p.PlanDay >= iStart && p.PlanDay <= iEnd);

                var result = await this.GetPageResultAsync(queryable, query);
                if (!search.Id.IsNullOrEmpty())
                {
                    if (!result.Data.Any(w => w.Id == search.Id))
                    {
                        var idOne = await this.GetQueryable(true).SingleOrDefaultAsync(w => w.Id == search.Id);
                        result.Data.Add(idOne);
                        result.Total += 1;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
            }
            return new PageResult<Plan>();
        }

        private PlanStatus GetStatus(int istatus)
        {
            PlanStatus result = PlanStatus.Executing;
            switch (istatus)
            {
                case 0:
                    result = PlanStatus.Staffing;
                    break;
                case 1:
                    result = PlanStatus.Issued;
                    break;
                case 2:
                    result = PlanStatus.Executing;
                    break;
                case 3:
                    result = PlanStatus.Completed;
                    break;
                case 4:
                    result = PlanStatus.ManualEnd;
                    break;
            }
            return result;
        }
    }
}
